{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 07\n",
    "\n",
    "# 鸡兔同笼-矩阵乘法\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "该代码使用线性代数的方法解决经典的“鸡兔同笼”问题。本质上，该问题可以被建模为一个 $2 \\times 2$ 线性方程组，并通过矩阵求逆的方法进行求解。\n",
    "\n",
    "---\n",
    "\n",
    "### **1. 设定线性方程组**\n",
    "设鸡的数量为 $x_1$，兔的数量为 $x_2$，我们得到如下两个方程：\n",
    "$$\n",
    "x_1 + x_2 = 35\n",
    "$$\n",
    "$$\n",
    "2x_1 + 4x_2 = 94\n",
    "$$\n",
    "这个方程组可以用矩阵的形式表示为：\n",
    "$$\n",
    "A \\mathbf{x} = \\mathbf{b}\n",
    "$$\n",
    "其中：\n",
    "$$\n",
    "A = \\begin{bmatrix} 1 & 1 \\\\ 2 & 4 \\end{bmatrix}, \\quad\n",
    "\\mathbf{x} = \\begin{bmatrix} x_1 \\\\ x_2 \\end{bmatrix}, \\quad\n",
    "\\mathbf{b} = \\begin{bmatrix} 35 \\\\ 94 \\end{bmatrix}\n",
    "$$\n",
    "矩阵 $A$ 是系数矩阵，向量 $\\mathbf{x}$ 是未知数，向量 $\\mathbf{b}$ 是右侧的常数列向量。\n",
    "\n",
    "---\n",
    "\n",
    "### **2. 计算矩阵 $A$ 的逆**\n",
    "解方程 $\\mathbf{x} = A^{-1} \\mathbf{b}$ 需要先计算矩阵 $A$ 的逆：\n",
    "$$\n",
    "A^{-1} = \\frac{1}{\\det(A)} \\operatorname{adj}(A)\n",
    "$$\n",
    "行列式 $\\det(A)$ 计算如下：\n",
    "$$\n",
    "\\det(A) = 1 \\cdot 4 - 1 \\cdot 2 = 2\n",
    "$$\n",
    "伴随矩阵（余子式矩阵的转置）：\n",
    "$$\n",
    "\\operatorname{adj}(A) = \\begin{bmatrix} 4 & -1 \\\\ -2 & 1 \\end{bmatrix}\n",
    "$$\n",
    "因此，矩阵 $A$ 的逆为：\n",
    "$$\n",
    "A^{-1} = \\frac{1}{2} \\begin{bmatrix} 4 & -1 \\\\ -2 & 1 \\end{bmatrix}\n",
    "= \\begin{bmatrix} 2 & -0.5 \\\\ -1 & 0.5 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "---\n",
    "\n",
    "### **3. 验证逆矩阵**\n",
    "代码通过计算 $A^{-1} A$ 和 $A A^{-1}$ 来验证矩阵 $A$ 的逆是否正确：\n",
    "$$\n",
    "A^{-1} A = \\begin{bmatrix} 2 & -0.5 \\\\ -1 & 0.5 \\end{bmatrix}\n",
    "\\begin{bmatrix} 1 & 1 \\\\ 2 & 4 \\end{bmatrix} = \n",
    "\\begin{bmatrix} 1 & 0 \\\\ 0 & 1 \\end{bmatrix}\n",
    "$$\n",
    "$$\n",
    "A A^{-1} = \\begin{bmatrix} 1 & 1 \\\\ 2 & 4 \\end{bmatrix}\n",
    "\\begin{bmatrix} 2 & -0.5 \\\\ -1 & 0.5 \\end{bmatrix} = \n",
    "\\begin{bmatrix} 1 & 0 \\\\ 0 & 1 \\end{bmatrix}\n",
    "$$\n",
    "这表明 $A^{-1}$ 计算正确。\n",
    "\n",
    "---\n",
    "\n",
    "### **4. 计算最终解**\n",
    "最终，使用 $A^{-1} b$ 计算未知数 $\\mathbf{x}$：\n",
    "$$\n",
    "\\mathbf{x} = A^{-1} \\mathbf{b} = \n",
    "\\begin{bmatrix} 2 & -0.5 \\\\ -1 & 0.5 \\end{bmatrix}\n",
    "\\begin{bmatrix} 35 \\\\ 94 \\end{bmatrix}\n",
    "$$\n",
    "进行矩阵乘法：\n",
    "$$\n",
    "x_1 = 2 \\times 35 + (-0.5) \\times 94 = 70 - 47 = 23\n",
    "$$\n",
    "$$\n",
    "x_2 = -1 \\times 35 + 0.5 \\times 94 = -35 + 47 = 12\n",
    "$$\n",
    "因此，鸡的数量是 **23**，兔的数量是 **12**。\n",
    "\n",
    "---\n",
    "\n",
    "### **总结**\n",
    "本代码基于矩阵运算求解 $2 \\times 2$ 线性方程组，核心步骤如下：\n",
    "1. 设定矩阵方程 $A \\mathbf{x} = \\mathbf{b}$。\n",
    "2. 计算 $A$ 的逆矩阵 $A^{-1}$。\n",
    "3. 验证 $A^{-1}$ 是否正确。\n",
    "4. 计算 $A^{-1} b$ 得到未知数的值，即 **鸡 23 只，兔 12 只**。\n",
    "\n",
    "这个方法是利用矩阵求逆来解线性方程组的一种常见方式，但在高维情况下，通常采用高斯消元法或 LU 分解等更高效的数值方法。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f98f12f8-64c9-4907-9910-7534106881b7",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d530bc5e-a5b5-41cf-92e4-461541be8fc2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcea75fb-c19a-4f90-a0e9-b8556344174b",
   "metadata": {},
   "source": [
    "## 鸡兔同笼系数矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f1044cb0-92c2-4e07-8b42-b5571dfc9101",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([[1, 1], \n",
    "              [2, 4]])\n",
    "# a_1, chicken \n",
    "# a_2, rabbit"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d759117-5643-4b86-a795-1ebf15aa254b",
   "metadata": {},
   "source": [
    "## 常数列向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "954b7525-dcef-47cb-af13-40c741ca3891",
   "metadata": {},
   "outputs": [],
   "source": [
    "b = np.array([[35],\n",
    "              [94]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7799aec4-4734-49f7-b0c4-2d34344aa7be",
   "metadata": {},
   "source": [
    "## 矩阵A的逆"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "aa3dd5d6-6437-4775-9611-64e4fbb5ef65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2. , -0.5],\n",
       "       [-1. ,  0.5]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_inv = np.linalg.inv(A)\n",
    "A_inv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "dd3bd71d-93b6-480f-bdbd-2b5bc85c8143",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0.],\n",
       "       [0., 1.]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 验证\n",
    "A_inv @ A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c6cdbfed-8757-4050-b184-844f094ccb31",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0.],\n",
       "       [0., 1.]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A @ A_inv"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb3be1b7-aad6-4a90-b890-06d7213c4edb",
   "metadata": {},
   "source": [
    "## 求解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ef4cdee3-8577-47e4-af0f-a2a78aec7401",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[23.],\n",
       "       [12.]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_inv @ b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03232b1c-9ff6-41a6-8eb9-b5f98c289f77",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "070c3389-8048-43a3-baa7-6666009bce96",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
